﻿using UnityEngine;

namespace FrameWork.Util.Expansion
{
    public static class TransformEx
    {
        public static void ResetLocal(this Transform transform)
        {
            if (!transform)
            {
                return;
            }
            
            transform.localPosition = Vector3.zero;
            transform.localRotation = Quaternion.identity;
            transform.localScale = Vector3.one;
        }

        public static void SetParentAndResetLocal(this Transform transform, Transform parent)
        {
            if (!transform)
            {
                return;
            }
            
            if (!parent)
            {
                return;
            }
            
            transform.SetParent(parent);
            transform.ResetLocal();
        }

        public static T GetOrAddComponent<T>(this GameObject obj) where T : Component
        {
            if (!obj)
                return null;
            
            var component = obj.GetComponent<T>();
            if(!component)
                component = obj.AddComponent<T>();
            
            return component;
        }
        
        public static T GetOrAddComponent<T>(this Transform transform) where T : Component
        {
            if (!transform)
                return null;
            
            var component = transform.GetComponent<T>();
            if(!component)
                component = transform.gameObject.AddComponent<T>();
            
            return component;
        }
    }
}